package com.antipov.odp.web;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: Andrew Antipov
 * Date: 19.02.14
 * Time: 13:44
 */
public class HelloWorldServlet extends HttpServlet {

	private static final int WIDTH = 640;
	private static final int HEIGHT = 120;
	private static final int FONT_SIZE = 72;
	private static final String HELLO_WORLD = "Hello, World";
	private static final int TOP = 100;
	private static final int LEFT = 100;
	private static final String FONT_NAME = "Times New Roman";

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/img");
		OutputStream out = response.getOutputStream();
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics gr = image.getGraphics();
		Random rg = new Random();
		Color color = new Color(rg.nextInt());
		gr.setColor(color);
		gr.setFont(new Font(FONT_NAME, Font.BOLD, FONT_SIZE));
		gr.drawString(HELLO_WORLD, LEFT, TOP);
		gr.dispose();
		ImageIO.write(image, "jpg", out);

	}


}
